﻿#include "dwgline.h"
#include <QtMath>
DwgLine::DwgLine(QObject *parent)
    : DwgObject{parent}
{

}


void DwgLine::SetData(QPointF p1, QPointF p2)
{
    mStartPt = p1;
    mEndPt = p2;
}
void DwgLine::Draw(QPainter *painter)
{
    if (mDeleteed) return;

    if (mSelected)
    {
        painter->setPen(Qt::red);
    }
    else
    {
        painter->setPen(Qt::black);
    }

    painter->drawLine(mStartPt, mEndPt);
}
void DwgLine::Select(QPointF pt)
{
    qreal dx = mEndPt.x() - mStartPt.x();
    qreal dy = mEndPt.y() - mStartPt.y();
    qreal lineLength = qSqrt(dx * dx + dy * dy);



    qreal distance = qAbs((dx * (mStartPt.y() - pt.y()) - dy * (mStartPt.x() - pt.x())) / lineLength);

    // 如果距离小于某个阈值（例如 5），则认为选中了直线
    if (distance < 3)
    {
        mSelected = true;

    }
    else
    {
        mSelected = false;

    }
}
